
namespace Core
{
    public class SceneMgr
    {
        public static int curScene { get; private set; } = -1;
        public static int curLevel { get; private set; }
        public static bool OpenScene(Scene scene, int sceneId)
        {
            curScene = sceneId;
            curLevel = 0;

            return LoadScene(scene, curScene, curLevel);
        }

        public static bool NextLevel(Scene scene)
        {
            var ret = LoadScene(scene, curScene, curLevel + 1);
            if (!ret)
                return false;
            curLevel++;
            return ret;
        }

        private static bool LoadScene(Scene scene,  int sceneId, int level)
        {
            var loader = World.GetComponent<ConfigComponent>().GetLoader<MapLevelConfigLoader>();
            if (!loader.HashScene(sceneId))
            {
                throw new System.Exception($"open scene {sceneId} fail!");
            }

            //Spawning.Instance.Stop();
            if (!loader.IsNext(sceneId, level))
                return false;
            
            var config = loader.GetConfig(sceneId, level);
            Log.Debug($"load scene: {sceneId}  level: {level}");
            //Spawning.Instance.StartSpawning(config);

            World.GetComponent<TimerComponent>().CreateTimer(config.durationTimer, () => NextLevel(scene));
            return true;
        }
    }

}